<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 靠右的按钮 -->
      <page-tools>
        <template v-slot:after>
          <el-button type="primary" size="small" @click="addPermission(1)">添加权限</el-button>
        </template>
      </page-tools>
      <!-- 表格 -->
      <el-table border :data="list" row-key="id">
        <el-table-column label="名称" prop="name" />
        <el-table-column align="center" label="标识" prop="code" />
        <el-table-column align="center" label="描述" prop="description" />
        <el-table-column align="center" label="操作">
          <template v-slot="{ row }">
            <el-button v-if="row.type === 1" type="text" @click="addPermission(row.id, 2)">添加</el-button>
            <el-button type="text" @click="editPermission(row.id)">编辑</el-button>
            <el-button type="text" @click="delPermission(row.id)">删除</el-button>
          </template>
        </el-table-column>

      </el-table>
    </div>
    <!-- 弹层 -->
    <el-dialog
      :title="showTitle"
      :visible="dialogVisible"
      @close="btnCancel"
    >
      <el-form ref="formData" :model="formData" :rules="rules" label-width="120px">
        <el-form-item label="权限名称" prop="name" style="width:90%">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code" style="width:90%">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述" style="width:90%">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="开启">
          <el-switch
            v-model="formData.enVisible"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <!-- 底部按钮 footer -->
      <template slot="footer">
        <el-row type="flex" justify="center">
          <el-col :span="10">
            <el-button size="small" type="primary" @click="btnOK">确 定</el-button>
            <el-button size="small" @click="btnCancel">取 消</el-button>
          </el-col>
        </el-row>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import { getPermissionList, delPermission, getPermissionDetail, updatePermission, addPermission } from '@/api/premission'
import { tranListToTreeData } from '@/utils'
import { getRequiredRules } from '@/utils/validate'
export default {
  name: '',
  components: {},
  props: {},
  data() {
    return {
      dialogVisible: false,
      list: [],
      formData: {
        name: '',
        code: '',
        description: '',
        pid: '',
        type: '',
        enVisible: '0'
      },
      rules: {
        name: [getRequiredRules('权限名')],
        code: [getRequiredRules('权限密码')]
      }
    }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '编辑权限' : '新增权限'
    }
  },
  watch: {},
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      this.list = tranListToTreeData(await getPermissionList(), '0')
      // const res = await getPermissionList()
      // console.log(res)
    },
    // 添加权限
    addPermission(pid, type) {
      this.formData.type = type
      this.formData.pid = pid
      this.dialogVisible = true
    },
    // 删除权限
    async delPermission(id) {
      try {
        await this.$confirm('确定要删除吗')
        await delPermission(id)
        this.getPermissionList()
        this.$message.success('删除成功')
      } catch (err) {
        console.log(err)
      }
    },
    // 编辑权限
    async editPermission(id) {
      this.formData = await getPermissionDetail(id)
      this.dialogVisible = true
    },
    // 确定按钮
    btnOK() {
      this.$refs.formData.validate(async isOK => {
        if (isOK) {
          if (this.formData.id) {
            await updatePermission(this.formData)
            this.$message.success('编辑成功')
          }
          await addPermission(this.formData)
          this.$message.success('新增成功')
        }
        this.getPermissionList()
        this.dialogVisible = false
      })
      // this.$refs.formData.validate().then(() => {
      //   // 判断是新增 还是编辑
      //   if (this.formData.id) {
      //     // 有id 去编辑
      //     return updatePermission(this.formData)
      //   }
      //   // 没id 去新增
      //   return addPermission(this.formData)
      // }).then(() => {
      //   this.$message.success('新增成功')
      //   this.getPermissionList()
      //   this.dialogVisible = false
      // })
    },
    btnCancel() {
      // 置空表单跟校验
      this.formData = {
        name: '',
        code: '',
        description: '',
        pid: '',
        type: '',
        enVisible: '0'
      }
      this.$refs.formData.resetFields()
      this.dialogVisible = false
    }
  }
}
</script>

<style scoped>

</style>
